package com.ma.workorder.contacts;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.widget.ImageView;

import com.ma.R;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import base.BaseActivity;
import bean.Depart;
import bean.Duty;
import bean.LoginBean;
import httpUtil.Api;
import httpUtil.Http;
import lomasky.xui.BaseViewHolder;
import lomasky.xui.ItemDivider;
import lomasky.xui.RecyclerViewAdapter;
import tellh.com.recyclertreeview_lib.TreeNode;
import tellh.com.recyclertreeview_lib.TreeViewAdapter;
import utils.ConstantUtil;
import utils.JsonUtil;

public class ChooseDepartActivity extends BaseActivity {
    private RecyclerView rv, recyclerView;
    private TreeViewAdapter adapter;
    private RecyclerViewAdapter<Duty> recyclerViewAdpter;
    private LoginBean loginBean;
    List<TreeNode> nodes = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginBean = (LoginBean) getIntent().getSerializableExtra(ConstantUtil.DATA1);
        setContentView(R.layout.activity_choose_depart);
        rv = (RecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new ItemDivider(this));
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        recyclerViewAdpter = new RecyclerViewAdapter<Duty>(this, R.layout.contacts_radio_item) {
            @Override
            public void onBind(BaseViewHolder holder, Duty item, int position) {

                holder.getRiadioButton(R.id.radio).setText(item.get_gwda_gwmc());
                holder.getRiadioButton(R.id.radio).setChecked(item.isChecked());


            }


        };
        recyclerViewAdpter.setOnItemClickListener((view, position) -> {
            for (int i = 0; i < recyclerViewAdpter.getData().size(); i++) {

                recyclerViewAdpter.getData().get(i).setChecked(i==position);
            }
            recyclerViewAdpter.notifyDataSetChanged();


        });
        recyclerView.setAdapter(recyclerViewAdpter);
        getDepart();

    }


    private void initView() {

//        contactsRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//
//        GridLayoutManager layoutManage = new GridLayoutManager(this, 2);
//        contactsRecyclerView.setLayoutManager(layoutManage);
//
//        recyclerViewAdpter = new RecyclerViewAdapter<Emp>(this, R.layout.contacts_radio_item) {
//            @Override
//            public void onBind(BaseViewHolder holder, Emp item, int position) {
//
//                holder.getRiadioButton(R.id.radio).setText(item.getName());
//                holder.getRiadioButton(R.id.radio).setChecked(item.isCheck());
//
//            }
//
//
//        };
//        List<Emp> emps = new ArrayList<>();
//
//        emps.add(new Emp("雇员1", false));
//        emps.add(new Emp("雇员2", false));
//        emps.add(new Emp("雇员3", false));
//        emps.add(new Emp("雇员4", false));
//        recyclerViewAdpter.setData(emps);
//
//
//        recyclerViewAdpter.setOnItemClickListener((view, position) -> {
//            for (int i = 0; i < emps.size(); i++) {
//                emps.get(i).setCheck(i == position);
//            }
//            recyclerViewAdpter.notifyDataSetChanged();
//
//
//        });
//        contactsRecyclerView.setAdapter(recyclerViewAdpter);

    }

    List<Depart> list;

    public void getDuty(String BMJG_ZJ) {
        showProgressDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("YH_ZJ", loginBean.getID());
        map.put("BMJG_ZJ", BMJG_ZJ);

        Http.post(Api.GetGWDAByBMJGWJ, map).subscribe(res -> {

            List<Duty>dutyList= JsonUtil.getList(res.getContent(),Duty.class);
            recyclerViewAdpter.setData(dutyList);
            recyclerViewAdpter.notifyDataSetChanged();
            closeProgressDialog();
        }, throwable -> {
            closeProgressDialog();
        });
    }


    public void getDepart() {
        showProgressDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("ID", loginBean.getID());
        Http.post(Api.GetBMJGTree, map).subscribe(res -> {
            list = JsonUtil.getList(res.getContent(), Depart.class);
            initData();
            adapter = new TreeViewAdapter(nodes, Arrays.asList(  new DirectoryNodeBinder()));
            adapter.setOnTreeNodeListener(new TreeViewAdapter.OnTreeNodeListener() {
                @Override
                public boolean onClick(TreeNode node, RecyclerView.ViewHolder holder) {
                    if (!node.isLeaf()) {
                        //Update and toggle the node.
                        onToggle(!node.isExpand(), holder);
//                    if (!node.isExpand())
//                        adapter.collapseBrotherNode(node);
                    }

                    Dir dir= (Dir) node.getContent();
                    toast(dir.getHref());
                    getDuty(dir.getHref());
                    return false;
                }

                @Override
                public void onToggle(boolean isExpand, RecyclerView.ViewHolder holder) {
                    DirectoryNodeBinder.ViewHolder dirViewHolder = (DirectoryNodeBinder.ViewHolder) holder;
                    final ImageView ivArrow = dirViewHolder.getIvArrow();
                    int rotateDegree = isExpand ? 90 : -90;
                    ivArrow.animate().rotationBy(rotateDegree)
                            .start();
                }
            });
            rv.setAdapter(adapter);
            closeProgressDialog();
        }, throwable -> {
            closeProgressDialog();
        });
    }

    private void initData() {

        for (Depart depart : list) {
            TreeNode<Dir> app = new TreeNode<>(new Dir(depart.getText(),depart.getHref()));
            if (depart.getNodes()!=null){
                for (Depart.Nodes nodes1 : depart.getNodes()) {
                    app.addChild(initDepart2( nodes1));
                }

            }
            nodes.add(app);
        }


    }

     TreeNode  initDepart2(  Depart.Nodes  nodes ) {
        TreeNode<Dir>  treeNode = new TreeNode<>(new Dir(nodes.getText(),nodes.getHref()));
        for (Depart.Nodes nodes1 : nodes.getNodes()) {

            TreeNode<Dir> node = new TreeNode<>(new Dir(nodes1.getText(),nodes1.getText()));
            treeNode.addChild(node);
            if (nodes1.getNodes()!=null){
                node.addChild(initDepart2(nodes1));

            }
        }

        return treeNode;

    }



}
